<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>游记明细</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/js/plugins/dialog/dialog.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/travelContent.css"/>
    <script src="/js/plugins/jrender/jrender.js"></script>
    <script src="/js/plugins/dialog/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <!--页面即将加载前加载-->
    <script>
        var travelId = getParams().id;
        $.get("/pageviews/"+travelId,function (data) {
            console.log(data+"-----");
            var pageview = JSON.parse(data);
            $("#pageview").html(pageview);
        })
    </script>
</head>
<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>
        </div>
        <div class="col">
        </div>
    </div>
</div>

<div class="travels">
    <img class="cover" render-src="coverUrl">
    <div class="container">
        <h6 class="title" render-html="title"></h6>
        <small>
            <span render-html="releaseTime"></span>
            回复 <span id="replyCount"> 100</span>
            浏览 <span id="pageview"> 100</span>
        </small>
        <div class="row">
            <div class="col-2 img">
                <a href="userProfiles.html" render-key="author.id" render-fun="getUser">
                    <img class="rounded-circle" render-src="author.headImgUrl">
                </a>
            </div>
            <div class="col-6 mine">
                <small>
                    <a href="userProfiles.html" render-key="author.id" render-fun="getUser">
                        <span render-html="author.nickName"></span>
                    </a>
                    <button class="btn" id="followBtn">关注</button>
                    <br>
                    <span>1000</span>篇游记
                    <span>1000</span>粉丝
                </small>
            </div>
            <div class="attention col">
                <span class="addr" render-html="place.name"></span>
            </div>
        </div>
    </div>
    ,
    <div class="container">
        <fieldset>
            <legend>我的出行信息</legend>
            <div class="d-flex">
                <div class="p-2  flex-fill">
                    <i class="fa fa-calendar"></i> 出发时间 <span render-html="travelTime"></span>
                </div>
                <div class="p-2  flex-fill"><i class="fa fa-money"></i> 人均费用
                    <span render-html="perExpends"></span>
                </div>
            </div>
            <div class="d-flex">
                <div class="p-2  flex-fill">
                    <i class="fa fa-user-circle"></i> 人物
                    <span render-html="personName"></span>
                </div>
                <div class="p-2  flex-fill"><i class="fa fa-clock-o"></i> 出行天数
                    <span render-html="days"></span>
                </div>
            </div>
        </fieldset>

        <div class="content" render-html="travelContent.content">
        </div>
    </div>

    <hr>

    <div class="container d-flex">
        <div class="p-2  flex-fill">
            <i class="fa fa-thumbs-o-up" id="likeBtn"></i>
        </div>
        <div class="p-2  flex-fill"><i class="fa  fa-commenting-o" id="commentBtn"> </i>
        </div>
        <div class="p-2  flex-fill"><i class="fa fa-star-o" style="color: red" id="collectBtn" > </i>
        </div>
    </div>
</div>
</body>
<script>
    // 页面加载完毕
    $(function () {

        // 获取地址栏的id值
        var id = getParams().id;

        // 获取用户信息
        var user = JSON.parse(sessionStorage.getItem("user"));

        if (id) {
            // 发送ajax请求获取游记信息
            $.get("/travels/" + id, function (data) {
                // 渲染数据
                $(".travels").renderValues(data,{
                    getUser:function (ele, value) {
                        ele.href = "/userProfiles.html?authorId=" + value;
                    }
                });
            });

            // 发送ajax请求获取游记点赞信息
            $.get("/travels/" + id + "/greats",{greatType: 'travel_id'}, function (data) {

                // 渲染点赞数据
                $("#likeBtn").html("&nbsp" + data.greats + "+");

            });

            // 发送ajax请求获取游记评论数
            $.get("/travels/" + id + "/replyCount", function (data) {

                // 渲染回复数据
                $("#commentBtn").html("&nbsp" + data.replyCount);
                $("#replyCount").html("&nbsp" + data.replyCount);

            });

            // 给点赞按钮绑定点击事件
            $("#likeBtn").click(function () {
                if (user) {
                    // 用户登录过, 发送ajax请求到后台进行点赞 或 取消点赞的处理
                    $.ajax({
                        url:"/travels/"+id+"/greats",
                        type:"POST",
                        data:{greatType: 'travel_id', userId: user.id},
                        success:function (data) {
                            // 渲染点赞数据
                            $("#likeBtn").html("&nbsp" + data.greats + "+");
                        }
                    });

                } else {
                    // 跳转到登录页面进行登录操作
                    $(document).dialog({
                        type: "confirm",
                        titleShow: false,
                        content: '您尚未登录',
                        buttons: [
                            {
                                name: '跳转到登录页面',
                                callback: function () {
                                    location.href = "/login.html";
                                }
                            }
                        ]
                    });
                }
            })
            if (user){
                // 查询是否收藏
                $.get("/collect/one",{userId:user.id,collectId:id,type:3},function (data) {
                    console.log(data);
                    //如果有值,则说明收藏
                    if (data){
                        $("#collectBtn").removeClass("fa-star-o");
                        $("#collectBtn").addClass("fa-star");
                    }else {
                        $("#collectBtn").removeClass("fa-star");
                        $("#collectBtn").addClass("fa-star-o");
                    }
                })
            }


            //定义一个变量最为dom对象
            var ele;
            //给收藏按钮绑定
            $("#collectBtn").click(function () {
                //拿到本身的标签赋值给dom对象
                ele = this;
                //判断是否有登录
                if (user){
                    //发起请求查看是否有收藏
                    $.ajax({
                        url:"/collect",
                        type:"post",
                        data:{userId:user.id,collectId:id,type:3},
                        success:function (data) {
                            //如果没有则没有颜色
                            if (data){
                                $(ele).removeClass("fa-star");
                                $(ele).addClass("fa-star-o");
                            }else {
                                //如果有则改变颜色
                                $(ele).removeClass("fa-star-o");
                                $(ele).addClass("fa-star");
                            }
                        }
                    });
                }else {
                    // 跳转到登录页面进行登录操作
                    $(document).dialog({
                        type: "confirm",
                        titleShow: false,
                        content: '您尚未登录',
                        buttons: [
                            {
                                name: '跳转到登录页面',
                                callback: function () {
                                    location.href = "/login.html";
                                }
                            }
                        ]
                    });
                }
            })

            //给评论符号绑定点击事件,跳转到全部评论页面
            $("#commentBtn").click(function () {
                window.location.href = "/travelReplies.html?travelId="+id;
            })



        }
    })
</script>

</html>